<body class="">
    <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">演示</a>
        <a>
          <cite>导航元素</cite></a>
      </span>
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
      <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so">
          <div class="layui-input-inline">
          <select name="type">
            <option value="username">用户名</option>
            <option value="phone">手机</option>
            <option value="email">邮箱</option>
          </select>
          </div>
          <input type="text" name="where"  placeholder="搜索" autocomplete="off" class="layui-input">
          <p class="layui-btn" id="search"><i class="layui-icon">&#xe615;</i></p>
        </form>
      </div>
      <table id="userlist" lay-filter="user"></table>

    </div>

  <script type="text/html" id="usertool">
    <div class="layui-btn-container">
      <button class="layui-btn layui-btn-sm  layui-btn-danger" lay-event="delall">批量删除</button>
    </div>
  </script>

  <script type="text/html" id="status">
    <input type="checkbox" name="status" value="{{d.uid}}" lay-skin="switch" {{ d.status =='1' ? '' : 'checked'}} lay-text="启用|停用" lay-filter="status">
  </script>

  <script type="text/html" id="userBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
  </script>
    <script>
      layui.use(['laydate','table','form'], function(){
        var laydate = layui.laydate;
        var table = layui.table;
        var form = layui.form;

        table.render({
          elem: '#userlist'
          ,url: '{:url('admin/user/index')}' //数据接口
          ,page: true //开启分页
          ,limit:10
          ,toolbar: '#usertool'
          ,title: '用户数据表'
          ,id:"userlist"
          ,cols: [[ //表头
             {type:'checkbox'}
            ,{field: 'uid', title: 'ID', width:80, sort: true}
            ,{field: 'username', title: '用户名',sort: true,minWidth:150}
            ,{field: 'sex', title: '性别', width:80}
            ,{field: 'email', title: '邮箱', width:150} 
            ,{field: 'phone', title: '手机', width: 120}
            ,{field: 'kiss', title: '积分', width: 80}
            ,{field: 'status', title: '状态',templet:"#status",width:100}
            ,{field: 'ctime', title: '注册时间', width: 200}
            ,{fixed: 'right',title:'操作', toolbar: '#userBar', width:120}
          ]]
        });

        //头工具栏事件
        table.on('toolbar(user)', function(obj){
          var checkStatus = table.checkStatus(obj.config.id);
          switch(obj.event){
            case 'delall':
              var data = checkStatus.data;

              uids = "";

              for(var i in data){

                  uids += data[i].uid+',';
              }

              $.post('{:url('admin/user/userdel')}', {uid: uids}, function(data, textStatus, xhr) {
                if(data.code==1){
                  $('.layui-laypage-btn').click();
                  layer.msg(data.msg,function () {})
                }else{
                  layer.msg(data.msg,function () {})
                }
              
              });

            break;
          };
        });

        table.on('tool(user)', function(obj){
          var data = obj.data;
          //console.log(obj)
          if(obj.event === 'del'){
            layer.confirm('真的删除行么', function(index){

              $.post('{:url('admin/user/userdel')}', {uid: data.uid}, function(data, textStatus, xhr) {
                if(data.code==1){
                  $('.layui-laypage-btn').click();
                  layer.msg(data.msg,function () {})
                }else{
                  layer.msg(data.msg,function () {})
                }
              });

              obj.del();
              layer.close(index);
            });
          } else if(obj.event === 'edit'){
              x_admin_show('用户编辑',"{:url('admin/user/edit')}?uid="+data.uid);
          }
        });

        //监听状态操作
        form.on('switch(status)', function(obj){

          $.post('{:url('admin/user/status')}', {uid: this.value,status:obj.elem.checked}, function(data, textStatus, xhr) {
              if(data.code==1){
                
                layer.msg(data.msg,function () {})
              }else{
                $('.layui-laypage-btn').click();
                layer.msg(data.msg,function () {})
              }
            });
        });
  
        $("#search").click(function(event) {
            //执行重载
            table.reload('userlist', {
              page: {
                curr: 1 //重新从第 1 页开始
              }
              ,where: {
                type:$('select[name=type]').val(),
                where:$('input[name=where]').val() 
              }
            });
        });
      })
    </script>
  </body>

</html>